package com.zenika.zenbinding.rebind;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class BindingParser {
  
  // FIXME cache the result?
  public static List<String> getBindableList() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    List<String> bindableList = new ArrayList<String>();
    try {
      DocumentBuilder builder = factory.newDocumentBuilder();
      File xml = new File("Binding.xml");
      Document document = builder.parse(xml);
      NodeList list = document.getElementsByTagName("bindable");
      for ( int i = 0; i < list.getLength(); i++ ) {
        Node node = list.item(i);
        NamedNodeMap map = node.getAttributes();
        bindableList.add(map.getNamedItem("class").getNodeValue());
      }
    }
    catch ( ParserConfigurationException e ) {
      e.printStackTrace();
    }
    catch ( SAXException e ) {
      e.printStackTrace();
    }
    catch ( IOException e ) {
      e.printStackTrace();
    }
    return bindableList;
  }
}
